import {
  cancelAttentionUser,
  attentionUser,
  getUserAttention,
  recommendUsers,
  isAttentionUser,
  isOperateArticle,
  getUserArticle,
  getUserRescueHotline
} from "@/service/user/user"
import type { IShowAttentions, IShowData } from "./type"
import { defineStore } from "pinia"

interface IUserState {
  showAttentions: IShowAttentions
  showSaveArticle: IShowData
  showSupportArticle: IShowData
  showPublishArticle: IShowData
  recommendUser: any[]
  isAttention: number
  isSupport: number
  isSave: number
  rescueHotline: any[]
}

const useUserStore = defineStore("user", {
  state: (): IUserState => ({
    showAttentions: {
      attentionLists: [],
      attentionTotal: 0
    },
    showPublishArticle: {
      lists: [],
      total: 0
    },
    showSaveArticle: {
      lists: [],
      total: 0
    },
    showSupportArticle: {
      lists: [],
      total: 0
    },
    recommendUser: [],
    isAttention: 0,
    isSupport: 0,
    isSave: 0,
    rescueHotline: []
  }),

  actions: {
    async getUserAttention(limit = 5, offset = 0) {
      const result = await getUserAttention(limit, offset)

      if (result.data.code == 200) {
        // console.log("获取关注数据", result.data.data)

        this.showAttentions = result.data.data
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getUserArticle(classify: "Support" | "Save" | "Publish", limit = 3, offset = 0) {
      const result = await getUserArticle(classify, limit, offset)

      if (result.data.code == 200) {
        console.log(classify, "获取数据", result.data.data)

        this[`show${classify}Article`] = result.data.data
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async recommendUsers(limit, uid) {
      let result
      if (!uid) {
        result = await recommendUsers(limit)
      } else {
        result = await recommendUsers(limit, uid)
      }

      // const result = await recommendUsers(limit, uid)

      if (result.data.code == 200) {
        // console.log("获取关注数据", result.data.data)

        this.recommendUser = result.data.data
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async attentionUser(uid) {
      const result = await attentionUser(uid)

      if (result.data.code == 200) {
        // console.log("关注用户", result.data)

        return Promise.resolve(result.data.message)
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async cancelAttentionUser(uid) {
      const result = await cancelAttentionUser(uid)

      console.log(result)

      // const result = await recommendUsers(limit, uid)

      if (result.data.code == 200) {
        // console.log("关注用户", result.data)

        return Promise.resolve(result.data.message)
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async isAttentionUser(uid) {
      const result = await isAttentionUser(uid)

      if (result.data.code == 200) {
        // console.log(result.data)

        this.isAttention = result.data.data.isAttention
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async isOperateArticle(operateFun: "Save" | "Support", article_id) {
      const result = await isOperateArticle(operateFun, article_id)

      if (result.data.code == 200) {
        // console.log(result.data)

        this[`is${operateFun}`] = result.data.data[`is${operateFun}`]
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getUserRescueHotline(province) {
      const result = await getUserRescueHotline(province)

      if (result.data.code == 200) {
        // console.log(result.data)
        this.rescueHotline = result.data.data
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    }
  },

  getters: {}
})

export default useUserStore
